package com.keba.lamp.ai.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.keba.basic.utils.ArgumentAssert;
import com.keba.lamp.ai.entity.AiApplication;
import com.keba.lamp.ai.service.AiApplicationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.keba.lamp.ai.service.AiEndPointService;
import com.keba.basic.base.service.impl.SuperServiceImpl;
import com.keba.lamp.ai.manager.AiEndPointManager;
import com.keba.lamp.ai.entity.AiEndPoint;
import com.keba.lamp.ai.vo.save.AiEndPointSaveVO;
import com.keba.lamp.ai.vo.update.AiEndPointUpdateVO;
import com.keba.lamp.ai.vo.result.AiEndPointResultVO;
import com.keba.lamp.ai.vo.query.AiEndPointPageQuery;

import java.util.Collection;

/**
 * <p>
 * 业务实现类
 * 接入点
 * </p>
 *
 * @author twg
 * @date 2025-03-02 19:38:54
 * @create [2025-03-02 19:38:54] [twg] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AiEndPointServiceImpl extends SuperServiceImpl<AiEndPointManager, Long, AiEndPoint> implements AiEndPointService {

    private final AiApplicationService aiApplicationService;


    @Override
    protected <SaveVO> AiEndPoint saveBefore(SaveVO saveVO) {
        AiEndPointSaveVO aiEndPointSaveVO = (AiEndPointSaveVO) saveVO;
        ArgumentAssert.isFalse(superManager.exists(Wrappers.lambdaQuery(AiEndPoint.class).eq(AiEndPoint::getName, aiEndPointSaveVO.getName()).eq(AiEndPoint::getCustomerId, aiEndPointSaveVO.getCustomerId())), "接入点名称已存在");
        return super.saveBefore(aiEndPointSaveVO);
    }


    @Override
    protected <UpdateVO> AiEndPoint updateBefore(UpdateVO updateVO) {
        AiEndPointUpdateVO aiEndPointUpdateVO = (AiEndPointUpdateVO) updateVO;
        ArgumentAssert.isFalse(superManager.exists(Wrappers.lambdaQuery(AiEndPoint.class).ne(AiEndPoint::getId, aiEndPointUpdateVO.getId()).eq(AiEndPoint::getName, aiEndPointUpdateVO.getName()).eq(AiEndPoint::getCustomerId, aiEndPointUpdateVO.getCustomerId())), "接入点名称已存在");
        return super.updateBefore(updateVO);
    }

    @Override
    public boolean removeByIds(Collection<Long> idList) {
        boolean isExists = aiApplicationService.getSuperManager().exists(Wrappers.lambdaQuery(AiApplication.class).in(AiApplication::getEndPointId, idList));
        ArgumentAssert.isFalse(isExists, "接入点已被应用使用，无法删除");
        return super.removeByIds(idList);
    }
}


